package com.qf.mybatis925.controller;

import com.qf.mybatis925.entity.User;
import com.qf.mybatis925.entity.VO.UserVO;
import com.qf.mybatis925.service.UserService;
import com.qf.mybatis925.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @GetMapping("/{id}")
    public R getUserById(@PathVariable Integer id){
        return R.ok(userService.getUserById(id));
    }
    @GetMapping("/all")
    public R getAllUser(){
        return R.ok(userService.getAllUser());
    }

    @PostMapping("/login")
    public R login(@RequestBody User user){
        if (userService.findUserByLogin(user)!=null){
            return R.ok(userService.findUserByLogin(user));
        }else return R.fail();
       /* return R.ok(userService.findUserByLogin(user));*/
    }

    @GetMapping("/search")
    public R search(@RequestParam HashMap<String,String> map) {
        List<User> list = userService.search(map);
        return R.ok(list);
    }

    @PostMapping("/delete")
    public R delete(@RequestBody User id){
        int i = userService.delete(id);
        if(i>0){
            return R.ok();
        }
        return R.fail();
    }

    @PostMapping("/edit")
    public R edit(@RequestBody User user){
        userService.updateById(user);
        return R.ok();
    }

    @PostMapping("/search2")
    public R search2(@RequestBody HashMap<String,String> map){
        List<User> list = userService.searchV2(map);
        return R.ok(list);
    }

    @PostMapping("/edit2")
    public R edit2(@RequestBody User user){
        userService.updateById2(user);
        return R.ok();
    }

//通过id查询用户订单
    @GetMapping("/findUserOrdersById")
    public R findUserOrdersById(Integer id){
        return R.ok(userService.findUserOrdersById(id));
    }
}
